<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>邮箱认证</title>

  {include file='common/_require'/}
</head>

<body>

  {include file='common/_header'/}
  <div class="layui-container fly-marginTop fly-user-main">
    {include file='common/_user'/}
    <script>$('.user-nav-item-set').addClass('layui-this')</script>

    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li class="layui-this" lay-id="info">邮箱认证</li>
        </ul>
        <div class="layui-tab-content" style="padding: 20px 0;">
          <div class="layui-form layui-form-pane layui-tab-item layui-show">
            <form method="post">

              <div class="layui-form-item">
                <label class="layui-form-label">当前邮箱</label>
                <div class="layui-input-inline">
                  <input type="text" name="old_email" readonly required lay-verify="required" autocomplete="off" value="{$user.email}" class="layui-input">
                </div>
              </div>
              {if $user.is_email_verify == 1 }

              <div class="layui-form-item">
                <label class="layui-form-label">新邮箱</label>
                <div class="layui-input-inline">
                  <input type="text" name="email" required lay-verify="required" autocomplete="off" value="" class="layui-input">
                </div>
                <div class="layui-form-mid send-message" style="color: #4f99cf;cursor: pointer;">发送验证邮件</div>
              </div>

              {/if}
              <div class="layui-form-item">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                  <input type="text" name="code" required lay-verify="required" autocomplete="off" value="" class="layui-input">
                </div>

              </div>

              <div class="layui-form-item">
                <button class="layui-btn" lay-filter="*" lay-submit>确认修改</button>
              </div>
          </div>

        </div>

      </div>
    </div>
  </div>
  {include file='common/_footer'/}
  <script>

    layui.use(['form'], function () {
      var form = layui.form

      form.on('submit(*)', function (data) {
        console.log(data);

        $.post('{:url("emailUpdate")}', data.field, function (result) {
          if (result.code == 0) {
            layer.msg('提交成功');

            setTimeout(() => {
              location.href = '{:url("set")}'
            }, 1200);
          } else {
            layer.msg(result.msg)
          }
        })

        return false
      })

      $('.send-message').click(function () {
        var item = this;
        if ($(item).text() != '发送验证邮件') {
          return false;
        }
        window.loading = layer.load();
        $.post('{:url("emailSendCode")}', {
          email: $('input[name="email"]').val()
        }, function (result) {
          layer.close(window.loading);
          if (result.code == 0) {
            layer.msg('发送成功,请注意查收')
            var second = 59;
            $(item).text(second + '秒后重新发送');
            var timer = setInterval(() => {
              $(item).text(second + '秒后重新发送');
              second--;

              if (second <= 1) {
                $(item).text('发送验证邮件')
                clearInterval(timer)
              }
            }, 1000);
          } else {
            layer.msg(result.msg)
          }
        })
      })

    })

  </script>
</body>

</html>